<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>证书管理</el-breadcrumb-item>
      <el-breadcrumb-item>成员管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入成员姓名、员工号、身份证号或公司名"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @change="searchMember"
            size="mini"
          >
            <el-button slot="append" icon="el-icon-search" @click="searchMember" size="mini"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="showAddMember" size="mini">添加成员</el-button>
        </el-col>
      </el-row>

      <!-- 显示成员列表的表格 -->
      <el-table :data="members" border stripe style="width: 100%" size="mini">
        <el-table-column type="index" label="#" width="60"> </el-table-column>
        <el-table-column prop="name" label="姓名" width="80"> </el-table-column>
        <el-table-column prop="p_num" label="员工号" width="80"> </el-table-column>
        <el-table-column prop="id_num" label="身份证号" width="160"> </el-table-column>
        <el-table-column prop="company" label="公司" width="60"> </el-table-column>
        <el-table-column prop="role" label="角色" width="120"> </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeMember(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[20, 30, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        size="mini"
      >
      </el-pagination>
    </el-card>
    <!-- 打开添加的对话框 -->
    <el-dialog title="添加成员" :visible.sync="addDialogVisible" @close="addDialogClose" ref="addMemberDialogRef" width="50%">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="成员姓名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="员工号" prop="p_num">
          <el-input v-model="addForm.p_num"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="id_num">
          <el-input v-model="addForm.id_num"></el-input>
        </el-form-item>
        <el-form-item label="所属公司" prop="company">
          <el-select v-model="addForm.company" clearable placeholder="请选择公司">
            <el-option v-for="item in company" :key="item.c_shortname" :label="item.c_shortname" :value="item.c_shortname"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成员角色" prop="role">
          <el-select v-model="addForm.role" clearable placeholder="请选择角色">
            <el-option v-for="item in role" :key="item.role" :label="item.role" :value="item.role"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMember">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 打开编辑成员的对话框 -->
    <el-dialog title="编辑成员信息" :visible.sync="editDialogVisible" @close="editDialogClose" ref="editMemberDialogRef" width="50%">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="成员姓名" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="员工号" prop="p_num">
          <el-input v-model="editForm.p_num"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="id_num">
          <el-input v-model="editForm.id_num"></el-input>
        </el-form-item>
        <el-form-item label="所属公司" prop="company">
          <el-select v-model="editForm.company" clearable placeholder="请选择公司">
            <el-option v-for="item in company" :key="item.c_shortname" :label="item.c_shortname" :value="item.c_shortname"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成员角色" prop="role">
          <el-select v-model="editForm.role" clearable placeholder="请选择角色">
            <el-option v-for="item in role" :key="item.role" :label="item.role" :value="item.role"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editMember">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 20
      },
      total: 10,
      addForm: {
        name: '',
        p_num: '',
        id_num: '',
        company: '',
        role: ''
      },
      addFormRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        id_num: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          { min: 18, max: 18, message: '身份证号长度为18个字符', trigger: 'blur' }
        ],
        company: [{ required: true, message: '请输入选择公司名', trigger: 'blur' }]
      },
      members: [],
      addDialogVisible: false,
      editDialogVisible: false,
      company: [],
      role: [],
      editForm: {
        id: '',
        id_num: '',
        name: '',
        p_num: '',
        id_num: '',
        company: '',
        role: ''
      },
      editFormRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        id_num: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          { min: 18, max: 18, message: '身份证号长度为18个字符', trigger: 'blur' }
        ],
        company: [{ required: true, message: '请输入选择公司名', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getMemberList()
    this.getBaseInfo()
  },
  methods: {
    showAddMember() {
      // this.getBaseInfo()
      this.addDialogVisible = true
    },
    async getBaseInfo() {
      const { data: res } = await this.$http.get('baseinfo/memberinfo')
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取添加成员必须的基本信息失败')
      }
      this.company = res.data.company
      this.role = res.data.role
    },
    addDialogClose() {
      this.$refs.addFormRef.resetFields()
    },
    // 添加成员
    addMember() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 发起请求添加member
        const { data: res } = await this.$http.post('liscense/addmember', this.addForm)
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.message)
        }
        this.$message.success(res.meta.message)
        this.addDialogVisible = false
        this.getMemberList()
      })
    },
    // 获取成员列表
    async getMemberList() {
      const { data: res } = await this.$http.get('liscense/getmember', { params: this.queryInfo })
      if (res.meta.status === 200) {
        this.members = res.data.page_members
        this.total = res.data.total
        this.queryInfo.pagenum = res.data.pagenum
        this.$message.success('获取成员信息成功！')
      } else if (res.meta.status === 201) {
        this.members = []
        this.total = 0
        this.queryInfo.pagenum = 1
        this.$message.info('未查询到相关数据！')
      } else {
        this.members = []
        this.total = 0
        this.queryInfo.pagenum = 1
        this.$message.info('查询过程中发生错误！')
      }
    },
    // 删除成员
    async removeMember(memberInfo) {
      const confirmResult = await this.$confirm('此操作将永久该成员信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await this.$http.delete('liscense/removemember/' + memberInfo.id)
      console.log(res.meta.status)
      if (res.meta.status !== 200) {
        return this.$message.error('成员删除失败！')
      }
      this.$message.success('成员删除成功！')
      this.getMemberList()
    },
    // 每页显示个数改变时处理函数
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getMemberList()
    },
    // 当前页码改变时的处理函数
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getMemberList()
    },
    // 点击查询按钮，模糊查询
    searchMember() {
      this.getMemberList()
    },
    // 编辑对话框关闭时的处理函数
    editDialogClose() {
      this.$refs.editFormRef.resetFields()
    },
    editMember() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        // 发起修改请求
        const { data: res } = await this.$http.put(`liscense/editmember/${this.editForm.id}`, this.editForm)
        // console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('用户信息修改失败！')
        }
        this.$message.success('用户信息修改成功！')
        this.editDialogVisible = false
        this.getMemberList()
      })
    },
    async showEditDialog(memberId) {
      const { data: res } = await this.$http.get('liscense/getmemberbyid/' + memberId)
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取待编辑的用户信息失败！')
      }
      this.editForm = res.data.member
      this.editDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-select {
  width: 100%;
}
</style>
